package com.prs.crm.action.oa.report;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.domain.oa.Regulation;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.service.oa.RegulationService;

public class RegulationReportAction extends ReportAction {
	private static final long serialVersionUID = -5447707093091558939L;
	private Date datestart;
	private Date dateend;
	private PaginationSupport pagination;
	private RegulationService regulationService;
	
	@Action("editRegulationReport")
	public String editRegulationReport(){
		return SUCCESS;
	}
	@Action("listRegulationReport")
	public String listRegulationReport(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Regulation.class);
		if(datestart != null){
			criteria.add(Restrictions.ge("creattime", datestart));
		}
		if(dateend != null){
			criteria.add(Restrictions.le("creattime", dateend));
		}
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRegulationService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action(value="exportRegulationReport", results = { @Result(name = "success"
		,type="stream",params={"contentType","${contentType}","inputName","inputStream"
		,"bufferSize","1024","contentDisposition","filename=${outfileName}"}) })
	public String exportRegulationReport(){
		ReportTable table = new ReportTable("规章制度报表");
		table.addCell(new ReportCell("规章制度编号","id"));
		table.addCell(new ReportCell("主题","title"));
		table.addCell(new ReportCell("登记时间","creattime"));
		table.addCell(new ReportCell("制度类型","type.name"));
		table.addCell(new ReportCell("适用部门","dept.name"));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Regulation.class);
		if(datestart != null){
			criteria.add(Restrictions.ge("creattime", datestart));
		}
		if(dateend != null){
			criteria.add(Restrictions.le("creattime", dateend));
		}
		this.setOutFilePath(this.getReportService().report(criteria, table, new ExportConfig(),this.getExportType()));
		return SUCCESS;
	}
	
	
	
	public Date getDatestart() {
		return datestart;
	}
	public void setDatestart(Date datestart) {
		this.datestart = datestart;
	}
	public Date getDateend() {
		return dateend;
	}
	public void setDateend(Date dateend) {
		this.dateend = dateend;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public RegulationService getRegulationService() {
		return regulationService;
	}
	public void setRegulationService(RegulationService regulationService) {
		this.regulationService = regulationService;
	}
	
}
